﻿Imports System.Data.SqlClient

''' <summary>
''' AC行當天行程表
''' </summary>
''' <remarks>
''' Create Date :2008/05/13
''' Create Person:huyang
''' </remarks>
Public Class Frm_CSACDeliver

    Implements IDServer.IDServer.IDServer

#Region "Variabled"

#Region "System Variabled Define"


    Public ActionHelp As IDServer.IDServer.ActionBtn = IDServer.IDServer.ActionBtn.ActionHelp

    Private WithEvents MainForm As Frm_MainForm

    Public Property SelectObject() As Frm_MainForm
        Get
            Return MainForm
        End Get
        Set(ByVal value As Frm_MainForm)
            MainForm = value
        End Set
    End Property

    Public action_help As IDServer.IDServer.ActionBtn = IDServer.IDServer.ActionBtn.ActionHelp

    Public FindBool As Boolean = True

#End Region

#Region "user variabled define"

    ''' <summary>
    ''' 窗體是否加載
    ''' </summary>
    ''' <remarks></remarks>
    Private fLoadBool As Boolean = False

    Public Fields As String

    Dim Masterdb As New DataTable

    Dim Detaildb As New DataTable

#End Region

#End Region

#Region "Methods"

#Region "Initialize"

    Private Sub Init()

        Me.dtp_date.Text = ServerDate
        Dim cs As setControlEnabled = New setControlEnabled(New Control() {Me.GroupBox2})
        cs.SetControlsEnabled(False)
        AddHandlers(New Control() {Me.GroupBox1, Me.GroupBox2})
        Dim ime As clsIme = New clsIme()
        ime.AddHandler_ImeMode(New Control() {Me.GroupBox1, Me.GroupBox2})

    End Sub

#End Region

#Region "ExecFind"

    Private Function ReturnWhereExp() As String

        Dim sql As String = ""

        If Me.fLoadBool = False Then
            sql &= " where 1<>1"
        Else

            sql &= " where 1=1  "

            sql &= "   and DeliverDate ='" & Format(Me.dtp_date.Value, "yyyy-MM-dd") & "'"

            If Me.txt_BoNum.Text <> "" Then
                sql &= " and BoNum like '%" & Me.txt_BoNum.Text.Trim & "%'"
            End If

        End If
        Return sql
    End Function

    Private Sub ExecFind(ByVal whereExp As String)
        Me.Cursor = Cursors.WaitCursor
        Dim ds As New DataSet

        Dim sql As String = ""

        'sql &= " declare @Table nvarchar(50)" & vbCrLf
        'sql &= " set @Table = newId()" & vbCrLf
        Dim tablename As String = Guid.NewGuid().ToString
        sql &= " select * into [#" + tablename + "] from CS_ACDeliverMaster " & whereExp
        sql &= " select * from [#" + tablename + "]" & vbCrLf
        sql &= " select * from CS_ACDeliverDetail where BoNum in (select Distinct BoNum from [#" + tablename + "])" & vbCrLf
        sql &= " drop Table [#" + tablename + "]" & vbCrLf

        Try

            ds = Exec(sql, E_ExecType.DataSet)
            Me.Masterdb = ds.Tables(0)
            Me.Detaildb = ds.Tables(1)

            Me.DGV_Master.DataSource = Detaildb
            BindDataGridViewCaption(Me.DGV_Master, Me.ReturnCaption(1))
            FullFormatDataGridView(Me.DGV_Master, False, False)
            Me.DGV_Master.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.DisplayedCells)
       
            Me.DGV_Master.SelectionMode = DataGridViewSelectionMode.CellSelect
            Dim isFind As Boolean = Me.DGV_Master.RowCount > 0

            If Me.fLoadBool = True AndAlso Me.FindBool = True Then
                SetButton(ButtonType.Browse, isFind)
            End If
            If Me.fLoadBool = False Then
                Me.DGV_Detail.DataSource = Detaildb.Clone()
                BindDataGridViewCaption(Me.DGV_Detail, Me.ReturnCaption(1))
                FullFormatDataGridView(Me.DGV_Detail, False, False)
                Me.DGV_Detail.SelectionMode = DataGridViewSelectionMode.CellSelect
                SetWidth()
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message & ex.StackTrace, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Public Function ReturnCaption(ByVal Index As Integer) As Hashtable
        Dim Hash As New Hashtable

        Select Case Index
            Case 1

                Hash.Add("ItemNo", "序號")
                Hash.Add("fSaleOrdeNo", "銷售單號")
                Hash.Add("fTimeRemark", "預約送貨時間")

        End Select

        Return Hash
    End Function

    Public Sub SetWidth()
        Me.DGV_Detail.Columns("ItemNo").Width = 50
        Me.DGV_Detail.Columns("fSaleOrdeNo").Width = 120
        Me.DGV_Detail.Columns("fTimeRemark").Width = 160
    End Sub

#End Region

#Region "SetEnabled"


    ''' <summary>
    ''' 設置按鈕狀態
    ''' </summary>
    ''' <param name="type">操作類型</param>
    ''' <param name="IsFind">是否是流覽</param>
    ''' <remarks></remarks>
    Private Sub SetButton(ByVal type As ButtonType, ByVal IsFind As Boolean)
        Try

            Select Case type
                Case ButtonType.Add
                    With MainForm
                        .ToolStripAdd.Enabled = False
                        .ToolStripEdit.Enabled = False
                        .ToolStripDelete.Enabled = False
                        .ToolStripSave.Enabled = True
                        .ToolStripCancel.Enabled = True
                        .ToolStripFirst.Enabled = False
                        .ToolStripLast.Enabled = False
                        .ToolStripNext.Enabled = False
                        .ToolStripPrivous.Enabled = False
                        .ToolStripExport.Enabled = IsFind
                        .ToolStripImport.Enabled = True
                        .ToolStripPrint.Enabled = IsFind
                        .ToolStripFilter.Enabled = True

                    End With

                Case ButtonType.Browse
                    With MainForm
                        .ToolStripAdd.Enabled = True
                        .ToolStripEdit.Enabled = IsFind
                        .ToolStripDelete.Enabled = IsFind
                        .ToolStripSave.Enabled = False
                        .ToolStripCancel.Enabled = False
                        .ToolStripFirst.Enabled = IsFind
                        .ToolStripLast.Enabled = IsFind
                        .ToolStripNext.Enabled = IsFind
                        .ToolStripPrivous.Enabled = IsFind
                        .ToolStripExport.Enabled = IsFind
                        .ToolStripImport.Enabled = True
                        .ToolStripPrint.Enabled = IsFind
                        .ToolStripFilter.Enabled = True
                        .ToolStripCheck.Enabled = False
                        .ToolStripComplete.Enabled = False
                        .ToolStripUnCheck.Enabled = False
                    End With

                Case ButtonType.Cancel
                    With MainForm
                        .ToolStripAdd.Enabled = True
                        .ToolStripEdit.Enabled = IsFind
                        .ToolStripDelete.Enabled = IsFind
                        .ToolStripSave.Enabled = False
                        .ToolStripCancel.Enabled = False
                        .ToolStripFirst.Enabled = IsFind
                        .ToolStripLast.Enabled = IsFind
                        .ToolStripNext.Enabled = IsFind
                        .ToolStripPrivous.Enabled = IsFind
                        .ToolStripExport.Enabled = IsFind
                        .ToolStripImport.Enabled = True
                        .ToolStripPrint.Enabled = IsFind
                        .ToolStripFilter.Enabled = True

                    End With

                Case ButtonType.Edit
                    With MainForm
                        .ToolStripAdd.Enabled = False
                        .ToolStripEdit.Enabled = False
                        .ToolStripDelete.Enabled = False
                        .ToolStripSave.Enabled = True
                        .ToolStripCancel.Enabled = True
                        .ToolStripFirst.Enabled = False
                        .ToolStripLast.Enabled = False
                        .ToolStripNext.Enabled = False
                        .ToolStripPrivous.Enabled = False
                        .ToolStripExport.Enabled = IsFind
                        .ToolStripImport.Enabled = True
                        .ToolStripPrint.Enabled = IsFind
                        .ToolStripFilter.Enabled = True

                    End With

            End Select
        Catch ex As Exception
            MsgBox(ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try

    End Sub



#End Region

#Region "AddHandler Events"

    ''' <summary>
    ''' KeyDown Event
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub TextBox_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
        Try
            Select Case e.KeyCode
                Case Keys.Enter
                    If e.Shift = True Then
                        If TypeOf sender Is ComboBox Then
                            If CType(sender, ComboBox).DroppedDown = True Then
                                CType(sender, ComboBox).DroppedDown = False
                            End If
                        End If
                        Me.ProcessTabKey(False)
                        SendKeys.Send("{F4}")
                    Else
                        If TypeOf sender Is ComboBox Then
                            If CType(sender, ComboBox).DroppedDown = True Then
                                CType(sender, ComboBox).DroppedDown = False
                            End If
                        End If

                        Me.ProcessTabKey(True)
                        SendKeys.Send("{F4}")

                    End If
            End Select
        Catch ex As Exception
            MsgBox("TextBox_KeyDown:" & ex.Message & ex.StackTrace, MsgBoxStyle.Critical And MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    ''' <summary>
    ''' 添加自定義事件
    ''' </summary>
    ''' <param name="Pans"></param>
    ''' <remarks></remarks>
    Private Sub AddHandlers(ByVal Pans() As Control)
        Try
            For Each Pan As Control In Pans
                For Each Ctrl As Control In Pan.Controls
                    If TypeOf Ctrl Is TextBox _
                       OrElse TypeOf Ctrl Is ComboBox _
                       OrElse TypeOf Ctrl Is DateTimePicker Then
                        AddHandler Ctrl.KeyDown, AddressOf TextBox_KeyDown
                    End If
                Next
            Next
        Catch ex As Exception
            MsgBox("AddHandlers:" & ex.Message & ex.StackTrace, MsgBoxStyle.Critical And MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

#End Region

#Region "FillDetail"

    Private Sub FillDetail(ByVal BoNum As String)
        Try
            Dim DRow() As DataRow = Me.Masterdb.Select("BoNum='" & BoNum & "'")

            If DRow.Length > 0 Then
                Me.txtBoNum.Text = DRow(0).Item("BoNum") & ""
                Me.dtpDeliverDate.Text = DRow(0).Item("DeliverDate") & ""
                Me.txtRemark.Text = DRow(0).Item("Remark") & ""
            Else
                Me.txtBoNum.Text = ""
                Me.dtpDeliverDate.Text = ""
                Me.txtRemark.Text = ""
            End If
            FillDetailGrid(BoNum)
        Catch ex As Exception
            MessageBox.Show(ex.Message & ex.StackTrace, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Public Sub FillDetailGrid(ByVal BoNum As String)
        Try
            Dim sql As String = ""

            sql &= "  select * from CS_ACDeliverDetail where BoNum='" & BoNum & "'"
            Dim Dtab As New DataTable
            Dtab = Exec(sql, E_ExecType.DataTable)
            Me.DGV_Detail.DataSource = Dtab

            BindDataGridViewCaption(Me.DGV_Detail, Me.ReturnCaption(1))
            FullFormatDataGridView(Me.DGV_Detail, False, False)
            Me.DGV_Detail.SelectionMode = DataGridViewSelectionMode.CellSelect
            SetWidth()
        Catch ex As Exception
            MessageBox.Show(ex.Message & ex.Source, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

#End Region

#Region "DataGridView"

    Private Sub SetDataGridEnabled(ByVal bEnabled As Boolean)
        Dim bReadOnly As Boolean = Not bEnabled

        Me.DGV_Detail.AllowUserToAddRows = bEnabled
        Me.DGV_Detail.AllowUserToDeleteRows = bEnabled
        Me.DGV_Detail.AllowUserToResizeRows = bEnabled


        Me.DGV_Detail.ReadOnly = bReadOnly
        If bEnabled Then
            Me.DGV_Detail.Columns("ItemNo").ReadOnly = True
        End If

    End Sub

#End Region

#Region "SaveData"

    Private Function Validated1() As Boolean
        'Me.DGV_Detail.Rows.Add()
        Me.DGV_Detail.EndEdit()
        Me.DGV_Detail.Update()


        Dim Dtab As DataTable = CType(Me.DGV_Detail.DataSource, DataTable)
        Dtab.AcceptChanges()

        Dim DRow() As DataRow = Dtab.Select("Isnull(fSaleOrdeNo,'')=''  and Isnull(fTimeRemark,'')=''")
        For Each row As DataRow In DRow
            row.Delete()
        Next
        Dtab.AcceptChanges()
        Me.DGV_Detail.Update()
        Me.DGV_Detail.Refresh()
        If Dtab.Rows.Count < 1 Then
            MessageBox.Show("沒有錄入數據，不能保存。", Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
            Return False
        End If
        Return True
    End Function

    Private Function SaveData() As Boolean

        If Me.Validated1() = False Then Return False

        Dim bSuccess As Boolean = False

        Dim BoNum As String = Me.txtBoNum.Text.Trim
        Dim DeliverDate As String = Format(Me.dtpDeliverDate.Value, "yyyy-MM-dd")
        Dim Remark As String = Me.txtRemark.Text.Trim
        Dim Master As DataTable = CType(Me.DGV_Detail.DataSource, DataTable)


        If ActionHelp = IDServer.IDServer.ActionBtn.ActionAdd Then
            BoNum = Exec("Exec ReturnBillNum 'CS_ACDeliverMaster','BoNum','DG'", E_ExecType.ExecuteScalar) & ""
            Me.txtBoNum.Text = BoNum
        End If

        If BoNum = "" Then
            MessageBox.Show("單號不能為空。", Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Warning)
            Return False
        End If

        Dim sql As String = ""

        If ActionHelp = IDServer.IDServer.ActionBtn.ActionAdd Then
            sql &= " Insert Into CS_ACDeliverMaster(BoNum,DeliverDate,Remark,fOperatorPsn,fOperatorDate,fOperatorComputer) " & vbCrLf
            sql &= " values('" & BoNum & "','" & DeliverDate & "','" & Remark & "','" & PubUserName & "',Convert(varchar(19),getdate(),21),'" & My.Computer.Name & "')" & vbCrLf
        Else
            sql &= " Update CS_ACDeliverMaster set "
            sql &= " DeliverDate='" & DeliverDate & "',"
            sql &= " Remark='" & Remark & "',"
            sql &= " fUpdatePsn='" & PubUserName & "',"
            sql &= " fUpdateDate=Convert(varchar(19),getdate(),21),"
            sql &= " fUpdateComputer='" & My.Computer.Name & "'"
            sql &= " where BoNum='" & BoNum & "' "

            sql &= " Delete CS_ACDeliverDetail where BoNum='" & BoNum & "'" & vbCrLf

        End If

        Dim Detail As DataTable = ReturnDetaildb(Master, BoNum)

        Dim SqlCN As SqlClient.SqlConnection
        SqlCN = GetConn()
        Dim SqlTran As SqlClient.SqlTransaction
        Dim SqlCmd As New SqlClient.SqlCommand
        Dim SqlBulk As SqlBulkCopy
        SqlTran = SqlCN.BeginTransaction

        Try
            With SqlCmd
                .CommandType = CommandType.Text
                .CommandText = sql
                .Connection = SqlCN
                .Transaction = SqlTran
                .ExecuteNonQuery()
            End With

            SqlBulk = New SqlClient.SqlBulkCopy(SqlCN, SqlBulkCopyOptions.Default, SqlTran)
            SqlBulk.DestinationTableName = "CS_ACDeliverDetail"
            SqlBulk.BulkCopyTimeout = 100
            SqlBulk.WriteToServer(Detail)
            SqlTran.Commit()
            bSuccess = True
        Catch ex As Exception
            SqlTran.Rollback()
            SqlCN.Close()
            SqlCN.Dispose()
            bSuccess = False
            MessageBox.Show(IIf(ActionHelp = IDServer.IDServer.ActionBtn.ActionAdd, My.resources.ErrAdd, My.resources.ErrEdit), Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
        If bSuccess = True Then
            MessageBox.Show(IIf(ActionHelp = IDServer.IDServer.ActionBtn.ActionAdd, My.Resources.AddSuccess, My.resources.EditSuccess), Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
            Dim whereExp As String = "  where BoNum='" & BoNum & "'"
            Me.ExecFind(whereExp)
        End If
        ActionHelp = IDServer.IDServer.ActionBtn.ActionHelp
        Dim cs As setControlEnabled = New setControlEnabled(New Control() {Me.GroupBox2})
        cs.SetControlsEnabled(False)
        Me.SetDataGridEnabled(False)
        Me.SetButton(ButtonType.Browse, bSuccess)
        Return True
    End Function

    Private Function ReturnDetaildb(ByVal Master As DataTable, ByVal BoNum As String) As DataTable
        Dim Dtab As New DataTable


        Dtab.Columns.Add("BoNum")
        Dtab.Columns.Add("ItemNo")
        Dtab.Columns.Add("fSaleOrdeNo")
        Dtab.Columns.Add("fTimeRemark")

        Dim I As Integer = 1
        For Each Row As DataRow In Master.Rows

            Dim NRow As DataRow = Dtab.NewRow()

            NRow("BoNum") = BoNum
            NRow("ItemNo") = I
            NRow("fSaleOrdeNo") = Row("fSaleOrdeNo")
            NRow("fTimeRemark") = Row("fTimeRemark")
            I += 1
            Dtab.Rows.Add(NRow)
        Next

        Dtab.AcceptChanges()

        Return Dtab
    End Function

#End Region

#End Region

#Region "Events"

#Region "Form Load"

    Private Sub Frm_CSACDeliver_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.fLoadBool = False
        Me.Init()
        Dim whereExp As String = Me.ReturnWhereExp()
        Me.ExecFind(whereExp)
        SetGridCaptionVisible(Me.DGV_Master, Fields, "Frm_CSACDeliver")
        Me.fLoadBool = True
    End Sub

    'Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean
    '    Try
    '        Const WM_KEYDOWN As Integer = &H100
    '        Const WM_SYSKEYDOWN As Integer = &H104
    '        If ((msg.Msg = WM_KEYDOWN) Or (msg.Msg = WM_SYSKEYDOWN)) Then
    '            Select Case keyData
    '                Case Keys.Enter
    '                    SendKeys.Send("{Tab}")
    '                    SendKeys.Send("{F4}")
    '                    Return True
    '                Case (Keys.Enter Or Keys.Shift)
    '                    SendKeys.Send("+{Tab}")
    '                    SendKeys.Send("{F4}")
    '                    Return True
    '            End Select
    '        End If
    '        Return MyBase.ProcessCmdKey(msg, keyData)
    '    Catch ex As Exception
    '        MessageBox.Show("ProcessCmdKey:" & ex.Message & ex.StackTrace)
    '    End Try
    'End Function

#End Region

#Region "Button"

    Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim whereExp As String = Me.ReturnWhereExp()
        Me.ExecFind(whereExp)
    End Sub

#End Region

#Region "DataGridView"

    Private Sub DGV_Master_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGV_Master.CellDoubleClick
        Me.TabControl1.SelectedIndex = 1
    End Sub

    Private Sub DGV_Detail_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles DGV_Detail.KeyDown
        Try
            If ActionHelp = IDServer.IDServer.ActionBtn.ActionAdd OrElse ActionHelp = IDServer.IDServer.ActionBtn.ActionEdit Then
                Select Case e.KeyCode
                    Case Keys.Delete
                        If Me.DGV_Detail.CurrentCell.IsInEditMode = False Then
                            If MsgBox(My.Resources.ConfirmDelete, MsgBoxStyle.Question + MsgBoxStyle.OkCancel, Msg.CompanyName) = MsgBoxResult.Ok Then
                                Dim DGView As DataGridView = DirectCast(sender, DataGridView)
                                DGView.Rows.RemoveAt(DGView.CurrentRow.Index)
                                DGView.Update()
                            End If
                        End If
                End Select
            End If
        Catch ex As Exception
            MsgBox("DGridView2_KeyDown:" & ex.Message & ex.StackTrace)
        End Try
    End Sub

#End Region

#Region "TableControl"

    Private Sub TabControl1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TabControl1.KeyDown
        Select Case e.KeyCode
            Case Keys.S
                If e.Control = True Then
                    If (ActionHelp = IDServer.IDServer.ActionBtn.ActionAdd OrElse ActionHelp = IDServer.IDServer.ActionBtn.ActionEdit) Then
                        MainForm.ToolStripSave.PerformClick()
                    End If
                End If
        End Select
    End Sub

    Private Sub TabControl1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TabControl1.SelectedIndexChanged
        If Me.TabControl1.SelectedIndex = 1 Then
            Dim BoNum As String = ""
            If Not Me.DGV_Master.CurrentRow Is Nothing Then
                BoNum = Me.DGV_Master.CurrentRow.Cells("BoNum").Value & ""
            End If
            Me.FillDetail(BoNum)
        End If
    End Sub

    Private Sub TabControl1_Selecting(ByVal sender As Object, ByVal e As System.Windows.Forms.TabControlCancelEventArgs) Handles TabControl1.Selecting
        If ActionHelp = IDServer.IDServer.ActionBtn.ActionAdd OrElse ActionHelp = IDServer.IDServer.ActionBtn.ActionEdit Then
            e.Cancel = True
        End If
    End Sub
#End Region

#Region "TextChanged"

    Private Sub txtfSaleOrdeNo_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtfSaleOrdeNo.TextChanged
        If Me.fLoadBool = False Then Exit Sub
        Dim dv As DataView = DirectCast(Me.DGV_Master.DataSource, DataTable).DefaultView
        Dim RowFilter = "1=1"
        If Me.txtfSaleOrdeNo.Text.Trim <> "" Then
            RowFilter = "fSaleOrdeNo like '%" & Me.txtfSaleOrdeNo.Text.Trim & "%'"
        End If
        dv.RowFilter = RowFilter
        Me.DGV_Master.Refresh()
    End Sub

#End Region

#End Region

#Region "Interface"

    Public Function ActionAdd() As Boolean Implements IDServer.IDServer.IDServer.ActionAdd
        Try
            If AddEnable = False Then
                MessageBox.Show("對不起，您沒有新增權限", Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
                Exit Function
            End If

            If Me.TabControl1.SelectedIndex = 0 Then
                Me.TabControl1.SelectedIndex = 1
            End If
            ActionHelp = IDServer.IDServer.ActionBtn.ActionAdd
            FillDetail("")
            Dim cs As setControlEnabled = New setControlEnabled(New Control() {Me.GroupBox2})
            cs.SetControlsEnabled(True)
            Me.txtBoNum.ReadOnly = True
            Me.DGV_Detail.Focus()
            Me.SetButton(ButtonType.Add, False)
            SetDataGridEnabled(True)
            Dim tb As DataTable = DirectCast(Me.DGV_Detail.DataSource, DataTable)
            Dim row As DataRow = tb.NewRow
            tb.Rows.Add(row)
            tb.AcceptChanges()
            Return True
        Catch ex As Exception
            MessageBox.Show(ex.Message & ex.StackTrace, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Function

    Public Function ActionCancel() As Boolean Implements IDServer.IDServer.IDServer.ActionCancel
        Try
            Dim BoNum As String = ""
            Dim IsFind As Boolean = False
            If Not Me.DGV_Master.CurrentRow Is Nothing Then
                BoNum = Me.DGV_Master.CurrentRow.Cells("BoNum").Value & ""
                IsFind = True
            End If
            Me.FillDetail(BoNum)
            Dim cs As setControlEnabled = New setControlEnabled(New Control() {Me.GroupBox2})
            cs.SetControlsEnabled(False)
            ActionHelp = IDServer.IDServer.ActionBtn.ActionHelp
            Me.SetButton(ButtonType.Browse, IsFind)
            SetDataGridEnabled(False)
            Return True
        Catch ex As Exception
            MessageBox.Show(ex.Message & ex.StackTrace, Msg.CompanyName)
        End Try
    End Function

    Public Function ActionDelete() As Boolean Implements IDServer.IDServer.IDServer.ActionDelete
        Try
            If DeleteEnable = False Then
                MessageBox.Show("對不起，您沒有刪除權限", Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
                Exit Function
            End If

            If Me.DGV_Master.CurrentRow Is Nothing Then Return False

            If MessageBox.Show(My.Resources.ConfirmDelete, Msg.CompanyName, MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) <> DialogResult.OK Then
                Return False
            End If
            Dim BoNum As String = Me.DGV_Master.CurrentRow.Cells("BoNum").Value & ""

            Dim sql As String = "  delete CS_ACDeliverMaster where BoNum='" & BoNum & "' " & vbCrLf
            sql &= "delete CS_ACDeliverDetail where BoNum='" & BoNum & "' " & vbCrLf

            Exec(sql, E_ExecType.ExecuteNonQuery)

            Dim Dtab As DataTable = CType(Me.DGV_Master.DataSource, DataTable)

            Dim DRow() As DataRow = Dtab.Select("BoNum='" & BoNum & "'")

            For Each row As DataRow In DRow
                row.Delete()
            Next
            Dtab.AcceptChanges()

            If Me.TabControl1.SelectedIndex = 1 Then
                Me.FillDetail("")
            End If
            Dim IsFind As Boolean = Me.DGV_Master.RowCount > 0
            Me.SetButton(ButtonType.Browse, IsFind)
            MessageBox.Show(My.Resources.DeleteSuccess, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
        Catch ex As Exception
            MessageBox.Show(My.Resources.ErrDelete, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Function

    Public Function ActionEdit() As Boolean Implements IDServer.IDServer.IDServer.ActionEdit
        Try
            If EditEnable = False Then
                MessageBox.Show("對不起，您沒有修改權限", Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
                Exit Function
            End If

            If Me.TabControl1.SelectedIndex = 0 Then
                Me.TabControl1.SelectedIndex = 1
            End If
            ActionHelp = IDServer.IDServer.ActionBtn.ActionEdit
            Dim cs As setControlEnabled = New setControlEnabled(New Control() {Me.GroupBox2})
            cs.SetControlsEnabled(True)
            Me.txtBoNum.ReadOnly = True
            Me.DGV_Detail.Focus()
            Me.SetButton(ButtonType.Edit, False)
            SetDataGridEnabled(True)
            Return True
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Function

    Public Function ActionExport() As Boolean Implements IDServer.IDServer.IDServer.ActionExport
        Me.Cursor = Cursors.WaitCursor
        Dim Grid As DataGridView = Me.DGV_Master
        Dim ds As New DataSet
        Dim Dtab As DataTable
        Dtab = DirectCast(Grid.DataSource, DataTable).Copy
        Dtab.TableName = "Master"
        RemoveHideColumn(Grid, Dtab)
        ds.Tables.Add(Dtab)
        SaveXls(ds)
        Me.Cursor = Cursors.Default
    End Function

    Public Function ActionFilter() As Boolean Implements IDServer.IDServer.IDServer.ActionFilter
        Try
            Dim Frm_Filter As New Frm_Filter
            Me.Cursor = Cursors.WaitCursor
            Frm_Filter.LoadHeaderCaption(Fields, "Frm_CSACDeliver")
            Frm_Filter.ShowDialog()
            Me.Cursor = Cursors.Default
            ReBindGridCaption(Me.DGV_Master, "Frm_CSACDeliver")
        Catch ex As Exception
            MsgBox("ActionFilter:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Function

    Public Function ActionGoFirst() As Boolean Implements IDServer.IDServer.IDServer.ActionGoFirst

    End Function

    Public Function ActionGoLast() As Boolean Implements IDServer.IDServer.IDServer.ActionGoLast

    End Function

    Public Function ActionGoNext() As Boolean Implements IDServer.IDServer.IDServer.ActionGoNext

    End Function

    Public Function ActionGoPrevious() As Boolean Implements IDServer.IDServer.IDServer.ActionGoPrevious

    End Function

    Public Function ActionImport() As Boolean Implements IDServer.IDServer.IDServer.ActionImport

    End Function

    Public Function ActionInitialize() As Boolean Implements IDServer.IDServer.IDServer.ActionInitialize

    End Function

    Public Function ActionPrint() As Boolean Implements IDServer.IDServer.IDServer.ActionPrint

    End Function

    Public Function ActionSave() As Boolean Implements IDServer.IDServer.IDServer.ActionSave
        Dim bSuccess As Boolean = SaveData()
        Return bSuccess
    End Function

    Public Function ActionSelect() As Boolean Implements IDServer.IDServer.IDServer.ActionSelect

    End Function

#End Region


    Private Sub GroupBox2_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GroupBox2.Enter

    End Sub
End Class
